#ifndef APPLAUNCHER_H
#define APPLAUNCHER_H
#include "classroomcomputersettings.h"
#include "random-students.h"
class UnclosableListWidget : public QListWidget
{
public:
	explicit UnclosableListWidget(QWidget *parent = nullptr) : QListWidget(parent) {}
protected:
	void closeEvent(QCloseEvent *event) override { event->ignore(); }
};
class DesktopBall : public PreventCloseWindow
{
	Q_OBJECT
public:
	DesktopBall(QWidget *parent = nullptr) : PreventCloseWindow(parent), isDragging(false)
	{
		setWindowFlags(Qt::Tool | Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint | Qt::WindowDoesNotAcceptFocus);
		setAttribute(Qt::WA_TranslucentBackground);
		setFixedSize(48, 48);
		move(screenx + screenw - width(), screeny + screenh - 200);
		svgRenderer.load(QString(":/img/desktopball.svg"));
#ifdef Q_OS_WIN
		QTimer *checkTopMostTimer = new QTimer(this);
		QObject::connect(checkTopMostTimer, &QTimer::timeout, [this]() { SetWindowPos(reinterpret_cast<HWND>(winId()), HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE); });
		checkTopMostTimer->start(1000);
#endif
	}
protected:
	void mousePressEvent(QMouseEvent *event) override
	{
		if (event->button() == Qt::LeftButton) {
			dragPosition = event->globalPosition().toPoint() - frameGeometry().topLeft();
			isDragging = true;
			emit clicked();
			event->accept();
		}
	}
	void mouseReleaseEvent(QMouseEvent *event) override
	{
		if (event->button() == Qt::LeftButton) {
			isDragging = false;
			event->accept();
		}
	}
	void mouseMoveEvent(QMouseEvent *event) override
	{
		if (isDragging && (event->buttons() & Qt::LeftButton)) {
			QPoint mousePos = event->globalPosition().toPoint();
			int newY = mousePos.y() - dragPosition.y();
			if (newY < screeny) newY = screeny;
			else if (newY + height() > screeny + screenh) newY = screeny + screenh - height();
			move(x(), newY);
			event->accept();
		}
	}
	void paintEvent(QPaintEvent *event) override
	{
		QPainter painter(this);
		painter.setRenderHint(QPainter::Antialiasing);
		QLinearGradient gradient(0, 0, width(), height());
		gradient.setColorAt(0, QColor(55, 131, 255, 200));
		gradient.setColorAt(1, QColor(165, 191, 214, 200));
		painter.setBrush(gradient);
		painter.setPen(Qt::NoPen);
		painter.drawEllipse(rect());
		if (svgRenderer.isValid()) {
			QRect targetRect((width() - 48) / 2, (height() - 48) / 2, 48, 48);
			svgRenderer.render(&painter, targetRect);
		}
	}
signals:
	void clicked();
private:
	bool isDragging;
	QPoint dragPosition;
	QSvgRenderer svgRenderer;
};
class AppLauncher : public PreventCloseWindow
{
	Q_OBJECT
public:
	AppLauncher(QWidget *parent = nullptr);
	~AppLauncher();
	CurrentClassInfo getCurrentClassInfo() const { return g_currentClassInfo; };
private slots:
	void handleItemClicked();
	void onDeviceDialogRejected();
private:
	const QStringList spinnerFrames = {"🕐", "🕑", "🕒", "🕓", "🕔", "🕕", "🕖", "🕗", "🕘", "🕙", "🕚", "🕛", "🕜", "🕝", "🕞", "🕟", "🕠", "🕡", "🕢", "🕣", "🕤", "🕥", "🕦", "🕧"};
	QMap<QPushButton *, QTimer *> buttonTimers;
	QMap<QPushButton *, int> buttonAnimationIndices;
	QMap<QPushButton *, QString> buttonOriginalTexts;
	QList<QPair<QString, char>> currentDrives;
	QPair<bool, QString> ejectRemovableDrive(char driveLetter);
	QWidget *centralWidget;
	QGridLayout *gridLayout;
	QJsonArray configItems;
	QDialog *deviceDialog = nullptr;
	QPushButton *currentTriggerButton = nullptr;
	QVector<QTime> m_startTimes, m_endTimes;
	QStringList m_courses, m_coursesTitles;
	QTimer *refreshTimer;
	QListWidget *timetableWidget;
	DesktopBall *desktopBall;
	ClassroomComputerSettings *SettingsWindow;
	RandomStudent *random_window;
	void refreshGUI();
	void showDeviceSelectionDialog(const QString &title, const QString &action);
	void showMessageOnButton(QPushButton *button, const QString &message);
	void restoreButtonAfterDelay(QPushButton *button, int delayMs);
	bool isEjectAction = false;
	int maxColumns = 4;
};
#endif
